<?php
class Error
{
	var $code;
	var $message_id;
	var $obj_id;
}

class ErrorList
{
	private $error		= array();
	private $message_id	= array();

	function Add($obj_id='', $field_caption='', $err_template=array('code'=>'', 'message_id'=>''))
	{
		if(is_array($obj_id) || is_object($obj_id)){
			$this->error	= array_merge($this->error, array($obj_id));
		}else{
			$ls_code = $err_template['code'];
			
			$ls_message_id = "";
			if(CULTURE == "id"){
				if(is_array($field_caption)){
					$ls_message_id = $err_template['message_id'];
					for($i=0, $max_i=count($field_caption); $i<$max_i; $i++){
						$ls_message_id = str_replace("{".$i."}", $field_caption[$i], $ls_message_id);
					}
				}else{
					$ls_message_id = str_replace("{0}", $field_caption, $err_template['message_id']);
				}
			}else{
				$ls_message_id = str_replace("{0}", $field_caption, $err_template['message_id']);
			}
			
			$this->Add(array('code' => $ls_code, 'message_id' => $ls_message_id, 'obj_id' => $obj_id));
		}
	}
	
	function Get()
	{
		return $this->error;
	}
	
	function GetDictionary()
	{
		$err_list = array();
		if (count($this->error) > 0)
		{
			foreach ($this->error as $err)
			{
				$obj_id				= $err['obj_id'];
				$err_list[$obj_id]	= $err['message_id'];
			}
		}
		return $err_list;
	}
	
	
	function addMessage($as_message_id){
		$this->message_id	= array_merge($this->message_id, array($as_message_id));
	}
	
	function getMessage(){
		$ls_message_id = '';
		$arr_message_id = $this->message_id;
		for($i=0,$max_i=count($arr_message_id); $i<$max_i; $i++){
			$ls_message_id .= $arr_message_id[$i]."\n";
		}
		return $ls_message_id;
	}
	
	function GetJSON()
	{
		$err_list = array();
		if (count($this->error) > 0)
		{
			foreach ($this->error as $err)
			{
				$err_list	= array_merge($err_list, array($err['obj_id'] => $err['message_id']));
			}
		}
		return json_encode($err_list);
	}
}
	
?>